package com.apetrenko.avaya.callstore.client;

import com.allen_sauer.gwt.voices.client.Sound;
import com.allen_sauer.gwt.voices.client.SoundController;
import com.allen_sauer.gwt.voices.client.handler.PlaybackCompleteEvent;
import com.allen_sauer.gwt.voices.client.handler.SoundHandler;
import com.allen_sauer.gwt.voices.client.handler.SoundLoadStateChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.Cell;

public class RecordsPanel extends Composite {

	private class DownloadHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent aEvent) {

		}

	}

	private class Player implements SoundHandler {

		private Sound iSound;
		private SoundController iSoundController;

		public Player() {
			iSoundController = new SoundController();
			iSoundController.setPrioritizeFlashSound(true);
		}

		public void play(long aID) {
			if (iSound != null) {
				iSound.stop();
			}
//			iSound = iSoundController.createSound(Sound.MIME_TYPE_AUDIO_X_WAV,
//					"callstore/play/" + aID, true);
			iSound = iSoundController.createSound(Sound.MIME_TYPE_AUDIO_X_WAV,
			"D:\\tmp\\test.spx", true);
			iSound.play();
			iSound.addEventHandler(this);
		}

		@Override
		public void onPlaybackComplete(PlaybackCompleteEvent aEvent) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onSoundLoadStateChange(SoundLoadStateChangeEvent aEvent) {
			// TODO Auto-generated method stub

		}

	}

	private class BtnListener implements ClickHandler {

		@Override
		public void onClick(ClickEvent aEvent) {
			if (iItem != null) {
				iPlayer.play(iItem.getId());

			}

		}

	}

	private class RecordsListener implements ClickHandler {

		@Override
		public void onClick(ClickEvent aEvent) {
			Cell vCell = iTable.getCellForEvent(aEvent);
			int vRow = vCell.getRowIndex();
			iItem = (Record) iTable.getItem(iTable.getDataRowIndex(vRow));
			if (iItem != null) {
				iPlayStopButton.setVisible(true);
				iDownloadLink.setHTML("<a href=callstore/download/" + iItem.getId() + ">Скачать</a>");
				iDownloadLink.setVisible(true);
			} else {
				iPlayStopButton.setVisible(false);
				iDownloadLink.setVisible(false);
			}

		}

	}

	private VerticalPanel iPanel = new VerticalPanel();
	private FlowPanel iButtonPanel = new FlowPanel();
	private BaseTable iTable = new BaseTable();

	private Hyperlink iPlayStopButton;
	private HTML iDownloadLink;
	private Record iItem;
	private Player iPlayer;

	RequestBuilder iRequest = new RequestBuilder(RequestBuilder.POST,
			"/callstore/download");

	public RecordsPanel() {
		iTable.addColumn("Файл");
		iTable.addColumn("Оператор");
		iTable.addColumn("Дата записи");
		iTable.addColumn("Длительность");
		iTable.addColumn("Вызывающий номер");
		iTable.setContentProvider(new RecordsContentProvider());
		iTable.setLabelProvider(new RecordsLabelProvider());

		iTable.setWidth("100%");

		iPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		iPanel.add(new HTML("Записи звонков"));

		iPanel.add(iTable);

		iPlayer = new Player();

		iButtonPanel.setWidth("100%");

		BtnListener vListener = new BtnListener();

		iPlayStopButton = new Hyperlink("Прослушать", "");
		iPlayStopButton.addClickHandler(vListener);

		iDownloadLink = new HTML();
		//iDownloadLink.addClickHandler(new DownloadHandler());

		iPlayStopButton.setVisible(false);
		iDownloadLink.setVisible(false);

		iButtonPanel.add(iPlayStopButton);
		iButtonPanel.add(iDownloadLink);

		iPanel.add(iButtonPanel);

		iTable.addClickHandler(new RecordsListener());

		initWidget(iPanel);

		setWidth("100%");
	}

}
